package com.ivc.info.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cs.framework.common.util.CommonUtils;
import com.cs.framework.jdbc.common.dao.CommonDao;
import com.ivc.info.dto.base.ClientDto;
import com.ivc.info.service.ClientService;

/**
 * 客户
 * 
 * @author hedongzhou
 * 
 */
@Service("clientService")
public class ClientServiceImpl implements ClientService {

	@Resource
	private CommonDao commonDao;

	/**
	 * 查询客户
	 * 
	 * @param clientNo
	 *            客户编号
	 * @return
	 */
	public ClientDto get(String clientNo) {
		return commonDao.getById(ClientDto.class, clientNo);
	}

	/**
	 * 查询客户总数
	 * 
	 * @param clientDto
	 * @return
	 */
	public int count(ClientDto clientDto) {
		return commonDao.count("Client.query", clientDto);
	}

	/**
	 * 查询客户列表
	 * 
	 * @param clientDto
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T extends ClientDto> List<T> query(T clientDto) {
		return (List<T>) commonDao.selectList("Client.query", clientDto, clientDto, clientDto.getClass());
	}

	/**
	 * 新增客户
	 * 
	 * @param clientDto
	 * @return
	 */
	public ClientDto save(ClientDto clientDto) {
		ClientDto entity = get(clientDto.getClientNo());
		if (entity != null) {
			CommonUtils.throwRun("该客户编号【" + clientDto.getClientNo() + "】已存在");
		}

		return commonDao.save(clientDto);
	}

	/**
	 * 修改客户
	 * 
	 * @param clientDto
	 * @return
	 */
	public ClientDto update(ClientDto clientDto) {
		ClientDto entity = get(clientDto.getClientNo());
		if (entity == null) {
			CommonUtils.throwRun("未查询到相关的客户信息");
		}

		CommonUtils.copyBoValue(entity, clientDto);

		return commonDao.update(entity);
	}

}
